<?php

require_once 'classes/LightPressPlugin.php';

class PostRelated extends LightPressPlugin {

    var $constructor_args = array(
        'howmany'=>'number of posts to show');
    var $default_context = LP_CONTEXT_POST;
    var $description = 'Display the n posts related to the current one';

    var $active = true;
    var $hooks = array('sidebar');
    
    var $howmany = 5;
    
    function PostRelated(&$frontend, $args, $dummy_run=false) {
        $this->LightPressPlugin($frontend, $args, $dummy_run);
    }
    
    function run($hook, &$hook_data) {
        $context = $this->_frontend->context;
        $db =& $this->_frontend->db;
        $tpl =& $this->_frontend->tpl;
        $post =& $this->_frontend->current_post;

        if (!$post)
            return $this->hide();

        $tpl->setFile('plugin_postrelated', 'plugins/post_related.xml', true);
        if (count($post['categories']) > 0 && $tpl->setBlock('plugin_postrelated', 'plugin_postrelated_post', 'PLUGIN_POSTRELATED_POST', false)) {

            $wc = implode(', ', $post['categories']);
            $tables =& $this->_frontend->tables;
            $q = "
                select distinct
                    p.id as post_id, p.post_name, p.post_title, p.post_category,
                    UNIX_TIMESTAMP(p.post_date) as tstamp
                from " . $tables['post2cat'] . " p2c
                inner join " . $tables['posts'] . " p on p.id=p2c.post_id
                where
                    p2c.category_id in ($wc) and
                    p.id != " . $post['post_id'] . " and
                    p.post_status='publish' and
                    p.post_date_gmt <= '" . $this->_frontend->where_date . "'
                order by
                    p.post_date desc limit " . $this->howmany;
            $db->query($q);
            $c = $db->count;
            
            if ($c == 0)
                return $this->hide();
            
            $separator =& $this->_frontend->options['category_separator'];
            $curr = 0;
            
            foreach ($db->all() as $related_post) {
                $curr++;
                $tpl->setVar(array(
                    'plugin_postrelated_post_name'     => $related_post['post_name'],
                    'plugin_postrelated_post_title'    => stripslashes($related_post['post_title']),
                    'plugin_postrelated_post_date'     => strftime($this->_frontend->options['short_date_format'], $related_post['tstamp']),
                    'plugin_postrelated_post_permalink'=> $this->_frontend->getPermalink($related_post['post_id'], $related_post['post_name'], $related_post['tstamp'], $related_post['post_category']),
                    'plugin_postrelated_post_separator'=>($curr == $c ? '' : $separator)
                    ));
                $tpl->parse('PLUGIN_POSTRELATED_POST', 'plugin_postrelated_post', true);
            }
            $tpl->parse('PLUGIN_POSTRELATED', 'plugin_postrelated');
        } else {
            return $this->hide();
        }

    }
    
}

?>